package com.econvent.common.log.utils;

import com.econvent.common.enums.constant.StringPool;
import com.econvent.common.log.entity.LogAbstract;
import com.econvent.common.secure.EconUser;
import com.econvent.common.secure.utils.AuthUtil;
import com.econvent.common.utils.UrlUtil;
import com.econvent.common.utils.WebUtil;
import org.apache.commons.lang3.ObjectUtils;

import javax.servlet.http.HttpServletRequest;

/**
 * Log 相关工具
 *
 * @author xiekun
 */
public class LogAbstractUtil {

	/**
	 * 向log中添加补齐request的信息
	 *
	 * @param request     请求
	 * @param logAbstract 日志基础类
	 */
	public static void addRequestInfoToLog(HttpServletRequest request, LogAbstract logAbstract) {
		if (ObjectUtils.isNotEmpty(request)) {
			logAbstract.setRemoteIp(WebUtil.getIP(request));
			logAbstract.setUserAgent(request.getHeader(WebUtil.USER_AGENT_HEADER));

			logAbstract.setRequestUri(UrlUtil.getPath(request.getRequestURI()));
			logAbstract.setMethod(request.getMethod());
			logAbstract.setParams(WebUtil.getRequestContent(request));

			EconUser econUser = AuthUtil.getUser(request);
			logAbstract.setCreateBy(econUser == null ? StringPool.EMPTY : econUser.getAccount());
			logAbstract.setCreateUser(econUser == null ? null : econUser.getUserId());
		}
	}
}
